package cn.jdy.platform.tag;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.SpringContextUtils;
import cn.jdy.plugins.entity.Website;
import cn.jdy.plugins.service.WebsiteService;

/**
 * 站点信息的自定义标签
 * @author zhaigt
 * @date 2015年6月26日
 */
public class SiteInfoTag extends TagSupport implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	private String var;// 属性名称
	
	@Override
	public int doEndTag()throws JspException  {
	  JspWriter out = pageContext.getOut();
	  
    WebsiteService websiteService = SpringContextUtils.getBean(WebsiteService.class);
    // 从数据库查询列表数据
    List<Website> list = websiteService.findAll();
    if(null == list || list.size() == 0){
        //如果没有查询到对象，则返回空字符串
        try {
          out.print("");
        } catch (IOException e) {
          BaseLog.e(this.getClass(), "获取站点数据-输出异常", e);
        }
      return EVAL_PAGE;
    }else{
  	  // 根据属性反射获取值
  		try {
  		  Website website = list.get(0);
  		  Field field = website.getClass().getDeclaredField(var);
  		  field.setAccessible(true);
  		  field.toString();
        out.print(field.get(website).toString());
      } catch (Exception e) {
        BaseLog.e(this.getClass(), "获取站点数据异常", e);
        try {
          out.print("");
        } catch (IOException e1) {
          BaseLog.e(this.getClass(), "获取站点数据-输出异常", e);
        }
      }  
  		// 标签的返回值
  		return EVAL_PAGE;
    }
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
	
}
